<?php
include_once 'bookLogin.php';

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/22
 * Time: 14:03
 */
function huanshu()
{

    //连接数据库
    $dsn = 'mysql:dbname=books;host=localhost';
    $user = 'root';
    $password = '123456';
    try {
        $db = new PDO($dsn, $user, $password);

        do {
            //输入卡号
            echo "请输入卡号：";
            $phone = trim(fgets(STDIN));

            //输入图书的编号
            echo "请输入图书编号：";
            $bookID = trim(fgets(STDIN));

            echo "\n";
            echo "输入y确认还书，任意键重新输入：";
            $re = trim(fgets(STDIN));
            $re = strtolower($re);

        } while ($re != 'y');

        $isreturntime = date("y-m-d");

        /*
        //删除用户信息数组
        $sqlDel = $db->prepare("delete from record where bookid='$bookID'");
        $sqlDel->execute();
       */
        $isreturnTime =date('Y-m-d');
        $sqlAdd = $db->prepare("update record set isreturnTime='$isreturnTime' where bookid='$bookID' and userid='$phone'");
        $sqlAdd->execute([
            ':isreturnTime' =>$isreturnTime
        ]);

        //获取影响行数
        $row = $sqlAdd->rowCount();

        //返回影响行数
        //如果影响行数大于等于1，则判断成功，否则失败
        if ($row >= 1) {
            echo "更新成功，受影响的行: 1";
            echo "\n";

            //提示还书成功
            echo "还书成功！\n";
            echo "\n";
        } else {
            echo "更新失败!\n";
            echo "\n";
            echo "还书失败！";
            echo "\n";
        }
        echo "\n";
        //调用bookLogin类中的jiinru()方法
        $jin = new bookLogin();
        echo $jin->moshi();
    } catch (PDOException $e) {
        echo 'Connection failed:' . $e->getMessage();
    }

}